package com.sgcc.pda.web.receiver;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.text.TextUtils;

import com.sgcc.pda.data.entity.BaseEntity;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.NetWorkTools;

import com.sgcc.pda.util.PlatConstant;
import com.sgcc.pda.web.IHttpCallbackInterface;

/**
 * @创建者：suven on 2020/3/24 10:55
 * @描述： 广播接收者
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public abstract class HttpCallback extends IHttpCallbackInterface.Stub implements IWebResult {

    private Context mContext;

    String mTag;

    public HttpCallback(Context context) {
        this.mContext = context;
        this.mTag = NetWorkTools.
                getRandomBoradcastName(context);
    }




    @Override
    public void onIDownLoading(int progress) {

    }



    @Override
    public String getTag() {
        return mTag;
    }

    @Override
    public void onSuccess(String result)  {
        if (TextUtils.isEmpty(result)) {
            LogPrintUtils.d(String.format("接口信息返回为空 %s", PlatConstant.IWebState.none));
            onIFail(PlatConstant.IWebState.none, "接口信息返回为空");
        } else {
            LogPrintUtils.d(String.format("result: %s", result));
            try {
                if (isDownLoad()) {
                    LogPrintUtils.d(String.format("成功 %s", PlatConstant.IWebState.success));
                    onISuccess(result);
                } else {
                    BaseEntity entity = BaseEntity.objectFromData(result, BaseEntity.class);
                    if (entity != null && !TextUtils.isEmpty(entity.RT_F)) {
                        if (PlatConstant.IWebState.login_timeout.equalsIgnoreCase(entity.RT_F)) {
                            LogPrintUtils.d(String.format("登录会话超时 %s", PlatConstant.IWebState.login_timeout));
                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent.setComponent(new ComponentName(PlatConstant.IWeb.PACKAGE_NAME, PlatConstant.IWeb.CLASS_NAME)));
                            System.exit(0);
                        } else if (PlatConstant.IWebState.success.equalsIgnoreCase(entity.RT_F)) {
                            LogPrintUtils.d(String.format("成功 %s", PlatConstant.IWebState.success));
                            onISuccess(result);
                        } else {
                            LogPrintUtils.d(String.format("%s %s", entity.RT_D, entity.RT_F));
                            onIFail(entity.RT_F, entity.RT_D);
                        }
                    } else {
                        LogPrintUtils.d(String.format("接口返回解析错误 %s", PlatConstant.IWebState.encrypterror));
                        onIFail(PlatConstant.IWebState.encrypterror, "接口返回解析错误");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogPrintUtils.d(String.format("接口返回产生异常 %s", PlatConstant.IWebState.error));
                LogPrintUtils.d(e);
                onIFail(PlatConstant.IWebState.error, "接口返回产生异常");
            }
        }
    }

    @Override
    public void onFailed(int errorCode, String errorMsg) throws RemoteException {
        onIFail(String.valueOf(errorCode), errorMsg);
    }

}
